package demo10_SynchronousQueue;

import java.util.concurrent.BlockingDeque;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;

/**
 * @author kunkun
 * @className Test
 * @Description SynchronousQueue
 * @date 2025/3/6 15:17
 */
public class Test {
    public static void main(String[] args) {
        BlockingQueue<Integer> queue = new SynchronousQueue<>();

        //1. 线程1往队列中添加元素
        new Thread(() -> {
            try {
                System.out.println(Thread.currentThread().getName()+"--> set-1");
                queue.put(1);

                System.out.println(Thread.currentThread().getName()+"--> set-2");
                queue.put(2);

                System.out.println(Thread.currentThread().getName()+"--> set-3");
                queue.put(3);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }, "t1").start();

        //2. 线程2从队列中获取元素
        new Thread(() -> {
            try {
                TimeUnit.SECONDS.sleep(3);
                System.out.println(Thread.currentThread().getName() + "====> get---"+queue.take());

                TimeUnit.SECONDS.sleep(3);
                System.out.println(Thread.currentThread().getName() + "====> get---"+queue.take());

                TimeUnit.SECONDS.sleep(3);
                System.out.println(Thread.currentThread().getName() + "====> get---"+queue.take());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "t2").start();
    }
}
